package com.boding.power.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.boding.power.common.Constants;
import com.boding.power.dto.CommentDto;
import com.boding.power.dto.UserDto;
import com.boding.power.model.base.BaseComment;
import com.boding.power.utils.DataGrid;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Comment extends BaseComment<Comment> {
	public static final Comment dao = new Comment();

	public DataGrid<CommentDto> findByPage(int page, int rows, String searchField,
			String searchString,Integer id){
		Page<Record> res = null;
		if (StringUtils.isNotBlank(searchField)
				&& StringUtils.isNotEmpty(searchField)
				&& StringUtils.isNotBlank(searchString)
				& StringUtils.isNotEmpty(searchString)) {
			res = Db
					.paginate(page,rows,
							"select u.username,c.*",
							"from comment c, users u where c.user_id = u.id and c.del_flag = 1 and c.goods_id = ? and "
									+ searchField
									+ " = ? order by c.create_date desc",
							id,searchString);
		} else {
			res = Db
					.paginate(page,rows,
							"select u.username,c.*",
							"from comment c, users u where c.user_id = u.id and c.del_flag = 1 and c.goods_id = ? order by c.create_date desc",id);
		}
		
		List<Record> list = res.getList();
		List<CommentDto> dtos = new ArrayList<CommentDto>();
		for (Record record : list) {
			CommentDto dto = new CommentDto();
			dto.setId(record.getInt("id"));
			dto.setCreate_date(record.getDate("create_date"));
			dto.setUsername(record.getStr("username"));
			dto.setContent(record.getStr("content"));
			dtos.add(dto);
		}
		DataGrid<CommentDto> dataGrid = new DataGrid<CommentDto>(res,dtos);
		return dataGrid;
	}
}
